<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
//$total_amount = 0.01 yh_payment2();
//$totalAmount = 0.01 create_url();
//create_url  信用卡，花呗

namespace Weixin\Controller;

use User\Api\UserApi;

/**
 * 用户控制器
 * 包括用户中心，用户登录及注册
 * yh_payment  行用卡最低付款额度测试修改为10
 */
class UserController extends WeixinController
{

    protected function _initialize()
    {
        /* 读取站点配置 */
        $config = api('Config/lists');
        C($config); //添加配置
        if (!C('WEB_SITE_CLOSE')) {
            $this->error('站点已经关闭，请稍后访问~');
        }
        $options = array(
            'token' => $this->token,   // 填写你设定的key
            'appid' => $this->appid,    // 填写高级调用功能的appid
            'appsecret' => $this->appsecret // 填写高级调用功能的密钥
        );
        $this->weixin = new \Common\Extend\Wechat\Wechat($options);

    }

    /* 用户中心首页 */
    public function index()
    {

    }


// 贷款使用------------------

    public function UserLoginOauth()
    {
        $result = $this->weixin->getOauthAccessToken();
        $this->WeixinLogin($result);
        $LoginGetUrl = cookie('LoginGetUrl');
        if (!empty($LoginGetUrl)) {
            $this->redirect($LoginGetUrl);
        } else {
            $this->redirect(U('Index/index'));
        }
    }

    protected function WeixinLogin($result)
    {
        $openid = $result['openid'];
        $expires_in = $result['expires_in'] - 60;
        $User = new UserApi;
        $Member = D('Member');
        $uid = $User->WeixinLogin($openid);
        if ($uid == -1) {
            $this->WeixinRegister($result);
        } else {
            $result['uid'] = $uid;
            //$this->updatenickname($result,$uid);
            $Member->login($uid);
            cookie('WeixinLogin', $result, $expires_in);
        }
    }

    protected function updatenickname($result, $uid)
    {
        $openid = $result['openid'];
        $access_token = $result['access_token'];
        $res = $this->weixin->getOauthUserinfo($access_token, $openid);
        $data = [
            'nickname' => $res['nickname'],
            'sex' => $res['sex'],
            'headimgurl' => $res['headimgurl'],
        ];
        $Member = D('Member');
        $Member->where('uid=' . $uid)->save($data);
    }

    /*
     *     用户微信注册
     */
    protected function WeixinRegister($result)
    {
        if (!C('USER_ALLOW_REGISTER')) {
            $this->error('注册已关闭');
        }
        $openid = $result['openid'];
        $User = new UserApi;
        $uid = $User->WeixinRegister($openid);
        if (0 < $uid) { //注册成功
            //TODO: 发送验证邮件
            //$this->success('注册成功！',U('login'));
            $expires_in = $result['expires_in'] - 60;
            $result['uid'] = $uid;
            $Member = D('Member');
            $Member->login($uid);
            $this->updatenickname($result, $uid);
            cookie('WeixinLogin', $result, $expires_in);
        } else { //注册失败，显示错误信息
            //$this->showRegError($uid);exit;
            $this->error($this->showRegError($uid));
        }
    }

    /**
     * 获取用户注册错误信息
     * @param  integer $code 错误编码
     * @return string        错误信息
     */
    private function showRegError($code = 0)
    {
        switch ($code) {
            case -1:
                $error = '用户名长度必须在16个字符以内！';
                break;
            case -2:
                $error = '用户名被禁止注册！';
                break;
            case -3:
                $error = '用户名被占用！';
                break;
            case -4:
                $error = '密码长度必须在6-30个字符之间！';
                break;
            case -5:
                $error = '邮箱格式不正确！';
                break;
            case -6:
                $error = '邮箱长度必须在1-32个字符之间！';
                break;
            case -7:
                $error = '邮箱被禁止注册！';
                break;
            case -8:
                $error = '邮箱被占用！';
                break;
            case -9:
                $error = '手机格式不正确！';
                break;
            case -10:
                $error = '手机被禁止注册！';
                break;
            case -11:
                $error = '手机号被占用！';
                break;
            case -12:
                $error = '微信被禁止注册！';
                break;
            case -13:
                $error = '微信号被占用！';
                break;
            default:
                $error = '未知错误';
        }
        return $error;
    }


    // 普通用户付款----post提交信用卡支付
    public function yh_payment()
    {

        $info = D('Tongdao')->find();


        if (IS_POST) {
            if ($info['hk_xy'] == 0) {
                echo "<script>confirm('该通道已关闭，不可付款');history.go(-1)</script>";
                die;
            }


            $uid = I('post.uid');
            $one = D('Ptyh')->where('uid=' . $uid)->find();
            if (empty($one['name'])) {
                echo "<script>confirm('请先绑定您的信息！');location.href='http://qnm.upslope.cn/index.php?m=Weixin&c=Koubei&a=yonghu'</script>";
                die;

            }


            $gather = D('Gather')->where("uid=" . $uid)->find();
            if ($gather['status_xy'] == 0) {
                echo "<script>alert('该商户无此收款权限！')</script>";
                die;
            }
            // if($uid!=8300){
            //     echo "<script>alert('该通道暂时关闭.')</script>";die;
            // }
            $one = D('Ptyh')->where("uid=" . $uid)->find();
            $total_amount = I('post.money');
            // 判断单笔累计、用户余额是否超出、支付宝当日累计支付
            if ($total_amount > 50000) {
                echo "<script>confirm('当前选择付款方式金额不可超过50000元！');history.go(-1)</script>";
                die;
            } else if (10 > $total_amount) {
                // 100-50000
                echo "<script>alert('当前选择付款方式金额不可低于100元。');history.go(-1)</script>";
                die;

            }
            $out_trade_no = 'xy' . date("YmdHis") . rand(1000, 9999);//2+14+4
            $subject = I('post.shop_name');

            $biz_content = [
                'productCode' => 'QUICK_WAP_PAY',
                'subject' => $subject,
                'out_trade_no' => $out_trade_no,
                'total_amount' => $total_amount,
                'timeout_express' => '5m',
                'enable_pay_channels' => 'creditCard,creditCardExpress,creditCardCartoon',//支持付款方式：信用卡
            ];
            // 记录一笔订单
            $data = [
                'uid' => $uid,
                'pay_time' => time(),
                'pay_money' => $total_amount,
                'danhao' => $out_trade_no,
                'status' => 0,
                'hk' => 1,
            ];
            D('Dingdan')->add($data);

            $api = new \Common\Extend\Api\ApiClient();
            $api = $api->alipay();
            $api->method = 'alipay.trade.wap.pay';
            $api->biz_content = json_encode($biz_content);
            $api->notify_url = 'http://qnm.upslope.cn/pay_return2.php';//异步通知
            $api->return_url = U('User/pay_return2', '', true, 'qnm.upslope.cn');//反回页页
            $data = $api->Form('POST', 'json')->submit();


        } else {

            if ($info['hk_xy'] == 0 && $info['hk_hb'] == 0 && $info['hk_fq'] == 0) {
                echo "<script>confirm('该通道已关闭，不可付款');history.go(-1)</script>";
                die;
            }


            $uid = $_GET['uid'];
            $one = D('Ptyh')->where('uid=' . $uid)->find();
            if (empty($one['name'])) {
                echo "<script>confirm('请先绑定您的信息！');location.href='http://qnm.upslope.cn/index.php?m=Weixin&c=Koubei&a=yonghu'</script>";
                die;

            }

            $gather = D('Gather')->where("uid=" . $uid)->find();
            // print_r($gather);die;
            if ($gather['status_xy'] == 0 && $gather['status_hb'] == 0 && $gather['status_fq'] == 0) {
                echo "<script>alert('该商户无此收款权限！')</script>";
                die;
            }

// if($uid!=8300){
// echo "<script>alert('该通道暂时关闭，稍后开放。')</script>";die;
// }


            if ($gather['status'] == 0) {
                echo "<script>alert('该收款码已停止收款功能。')</script>";
                die;
            }
            $this->assign('one', $one);
            C('WEB_SITE_TITLE', $one['shop_name']);
            $this->display();
        }
    }

    // 普通用户付款----post提交信用卡支付
    public function yh_payment3()
    {
        // echo "<script>alert('该通道1小时后开放，请谅解！');</script>";die;
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'AlipayClient') === false) {
            echo "<script>confirm('请使用支付宝打开~');</script>";
            die;
        }
        $info = D('Tongdao')->find();

        if (IS_POST) {


        } else {
            $info = D('Tongdao')->find();
            if ($info['hk_xy'] == 0 && $info['hk_hb'] == 0 && $info['hk_fq'] == 0) {
                echo "<script>confirm('该通道已关闭，不可付款');history.go(-1)</script>";
                die;
            }
            $uid = $_GET['uid'];


//                if($uid!='8300'){
//     echo "<script>alert('付款通道暂做维护，请谅解！');</script>";die;
// }


            if (empty($uid) || $uid == 0) {
                echo "<script>confirm('该用户不存在！');location.href='http://qnm.upslope.cn/index.php?m=Weixin&c=Koubei&a=yonghu'</script>";
                die;
            }
            $one = D('Ptyh')->where('uid=' . $uid)->find();
            if (empty($one['name'])) {
                echo "<script>confirm('请先绑定您的信息！');location.href='http://qnm.upslope.cn/index.php?m=Weixin&c=Koubei&a=yonghu'</script>";
                die;

            }


            $gather = D('Gather')->where("uid=" . $uid)->find();
            // print_r($gather);die;
            if ($gather['status_xy'] == 0 && $gather['status_hb'] == 0 && $gather['status_fq'] == 0) {
                echo "<script>alert('该商户无此收款权限！')</script>";
                die;
            }


            if ($gather['status'] == 0) {
                echo "<script>alert('该收款码已停止收款功能。')</script>";
                die;
            }
            $one = D('Ptyh')->where("uid={$uid}")->find();
            $this->assign('one', $one);
            C('WEB_SITE_TITLE', $one['shop_name']);
            $this->display();
        }
    }


    // 普通用户付款----post提交信用卡支付

    /**
     * 收款码支付宝扫码 -- 信用卡支付
     */
    public function yh_payment2()
    {
        // if (strpos($_SERVER['HTTP_USER_AGENT'], 'AlipayClient') === false) {
        //         echo "<script>confirm('请使用支付宝打开~');</script>";die;
        // }

        $info = D('Tongdao')->find();
        if (IS_POST) {
            $uid = I('post.uid');
            $shoukuanma_id = I('post.shoukuanma_id'); //收款码id
            // if($uid!=2){
            //     echo "暂不开放";die;
            // }

            if (empty($uid) || $uid == 0) {
                echo "该商户不存在！";
                die;
            }
            $one = D('Ptyh')->where('uid=' . $uid)->find();
            if (empty($one['name'])) {
                echo "该商户不存在！";
                die;

            }
            $gather = D('Gather')->where('uid=' . $uid)->find();


            if ($uid == 2) {

            } else {
                if ($info['hk_xy'] == 0) {
                    echo "该通道已关闭，不可付款";
                    die;
                }
            }

            $total_amount = I('post.money');

            if ($total_amount > 50000) {
                echo "当前选择付款方式金额不可超过50000元！";
                die;
            } else if (10 > $total_amount) {
                // 1-50000
                echo "当前选择付款方式金额不可低于10元。";
                die;

            }
            $out_trade_no = 'xy' . date("YmdHis") . rand(1000, 9999);//2+14+4
            // $subject        = I('post.shop_name');
            $subject = '到店付' . $out_trade_no;

            // 记录一笔订单
            $data = [
                'uid' => $uid,
                'pay_time' => time(),
                'pay_money' => $total_amount,
                'danhao' => $out_trade_no,
                'status' => 0,
                'hk' => 0, //回款
            ];
            D('Dingdan')->add($data);

            // 手机网站支付
            Vendor('alipay.trade.wappay.pay');
            $api = new \Alipay;
            $alipays = D('Alipays')->where(['status' => 1, 'type' => 1])->select();

            $arr = array();

            // print_r($alipays);  //随机抽取一个可用的支付宝来收款
            $time1 = strtotime(date("Ymd", strtotime("+1 day")));
            $time2 = strtotime(date("Ymd"));
            foreach ($alipays as $key => $v) {
                $seller = $v['account'];
                $sum = D('Dingdan')->where("seller='$seller' and pay_time between $time2 and $time1")->sum('pay_money');
                if ($sum + $total_amount < 1000000) {
                    $arr[] = $v['id'];
                }

            }
            $id = array_rand($arr, 1);
            $alipay = $alipays[$id];

//            $total_amount = 0.01; //测试修改为0.01
            $result = $api->xy_pay($out_trade_no, $subject, $total_amount, $alipay);
            print_r($result);
            exit;
        } else {
            if ($info['hk_xy'] == 0 && $info['hk_hb'] == 0 && $info['hk_fq'] == 0) {
                echo "<script>confirm('该通道已关闭，不可付款');history.go(-1)</script>";
                die;
            }

            $uid = $_GET['uid'];
            $shoukuanma_id = $_GET['id']; //收款码id
            if (empty($uid) || $uid == 0) {
                echo "<script>confirm('该用户不存在！');location.href='http://qnm.upslope.cn/index.php?m=Weixin&c=Koubei&a=yonghu'</script>";
                die;
            }
            $gather = D('Gather')->where("uid=" . $uid)->find();
            $one = D('Ptyh')->where('uid=' . $uid)->find();
            if (empty($one['name'])) {
                echo "<script>confirm('请先绑定您的信息！');location.href='http://qnm.upslope.cn/index.php?m=Weixin&c=Koubei&a=yonghu'</script>";
                die;

            }


            if ($gather['status'] == 0) {
                echo "<script>alert('该收款码已停止收款功能。')</script>";
                die;
                // print_r('该收款码已停止收款功能。');die;
            }
            $one = D('Ptyh')->where("uid={$uid}")->find();
            $this->assign('one', $one);
            $this->assign('shoukuanma_id', $shoukuanma_id);
            C('WEB_SITE_TITLE', $one['shop_name']);
            $this->display();
        }
    }

    /**
     * 收款码支付宝扫码-- 付款方式花呗支付，信用卡
     * pay_type = 2 花呗，其他信用卡
     */
    function create_url()
    {
        if (IS_POST) {
            $info = D('Tongdao')->find();
            if ($info['hk_hb'] == 0 && $info['hk_fq'] == 0) {
                echo json_encode(['info' => '该通道已关闭，不可付款']);
                die;
            }

            $uid = I('post.uid');
            $pay_type = I('post.pay_type');
            $gather = D('Gather')->where("uid=" . $uid)->find();

            if (empty($uid) || $uid == 0) {

                echo json_encode(['info' => '该用户不存在！']);
                die;
            }

            $totalAmount = I('post.money');//交易金额
            if ($pay_type == 2) {
                //花呗

                if ($info['hk_hb'] == 0) {
                    echo json_encode(['info' => '该通道已关闭，不可付款']);
                    die;
                }

                if ($totalAmount < 10) {  //100-15000

                    echo json_encode(['info' => '当前选择付款方式金额不可低于10元！']);
                    die;
                } else if ($totalAmount > 499) {
                    echo json_encode(['info' => '当前选择付款方式金额不可超过499元！']);
                    die;
                }

            } else {
                //信用卡
                // 判断通道是否关闭
                if ($info['hk_fq'] == 0) {
                    echo json_encode(['info' => '该通道已关闭，不可付款']);
                    die;
                }


                if ($totalAmount < 100) {  //100-15000

                    echo json_encode(['info' => '当前选择付款方式金额不可低于10元！']);
                    die;
                } else if ($totalAmount > 15000) {
                    echo json_encode(['info' => '当前选择付款方式金额不可超过15000元！']);
                    die;
                }
            }
//            $adata = D('Adata')->where(['id' => 1])->find();
            $adata = M('shouquan')->find();

            if ($pay_type == 2) {
                // 花呗

                // 从口碑商户里面随机取出一个已授权、未过期的令牌来使用，当seller_id是这个口碑商户的user_id时，说明订单使用了这个口碑商户令牌
                // if($totalAmount<501){
                $type = 1;
                // }else if($totalAmount>500&&$totalAmount<801){
                //     $type=2;
                // }else if($totalAmount>800&&$totalAmount<2001){
                //     $type=3;
                // }else if($totalAmount>2000){
                //     $type=4;
                // }

                if ($type == '') {
                    $type = 1;
                }


                $list = D('Kbsh')->select();
                $arr = array();
                foreach ($list as $key => $v) {
                    $time1 = $v['create_time'] + $v['expires_in'];
                    $time2 = time();
                    if ($v['status'] == 1 && $v['use_status'] == 1 && $time1 > $time2 && $v['type'] == $type) {
                        // if($v['status']==1&&$v['use_status']==1&&$time1>$time2){
                        $arr[] = $v;
                    }
                }
                $id = array_rand($arr, 1);
                $one = $arr[$id];
                $app_auth_token = $one['app_auth_token'];
                // echo $app_auth_token;die;
                $outTradeNo = 'hb' . date("YmdHis") . rand(1000, 9999);//2+14+4
                // $subject=I('post.shop_name');//
                $subject = "到店付$outTradeNo";
                // 调用预下单接口得到付款二维码
                Vendor('dangmianfu_demo_php.f2fpay.qrpay_test');
                $qrpay = new \Alipay;

//                $totalAmount = 0.01;
                $result = $qrpay->qrpay($outTradeNo, $subject, $totalAmount, $app_auth_token, $adata);
                // print_r($result);die;


                $erwm = $result->qr_code;
                if ($erwm == '') {
                    // 该口碑码不可用
                    D('Kbsh')->where(['app_auth_token' => $app_auth_token])->save(['use_status' => 0, 'cause' => '该码不可用']);
                    echo json_encode(['info' => '请刷新重试！']);
                    die;
                }
                // 保存数据
                $data = [
                    'uid' => $uid,
                    'danhao' => $outTradeNo,
                    'pay_money' => $totalAmount,
                    // 'pay_time'=>time(),
                    'pay_type' => $pay_type,
                    'seller_id' => $one['user_id'],
                    'erwm' => $erwm,
                ];
                echo json_encode($data);
                die;


            } else if ($pay_type == 3) {
                // 花呗分期
                $list = D('Kbsh')->select();
                $arr = array();
                foreach ($list as $key => $v) {
                    $time1 = $v['create_time'] + $v['expires_in'];
                    $time2 = time();
                    if ($v['status'] == 1 && $v['use_status'] == 1 && $time1 > $time2) {
                        $arr[] = $v;
                    }
                }
                $id = array_rand($arr, 1);
                $one = $arr[$id];
                $app_auth_token = $one['app_auth_token'];

                $pcreate = I('post.pcreate');
                if ($pcreate == 3) {   //构造分期数
                    $extend_params = array('hb_fq_num' => '3', 'hb_fq_seller_percent' => '0');
                } else if ($pcreate == 6) {
                    $extend_params = array('hb_fq_num' => '6', 'hb_fq_seller_percent' => '0');

                } else if ($pcreate == 12) {
                    $extend_params = array('hb_fq_num' => '12', 'hb_fq_seller_percent' => '0');
                }

                $outTradeNo = 'fq' . date("YmdHis") . rand(1000, 9999);//2+14+4
                $subject = "到店付$outTradeNo";


                // 调用预下单接口得到付款二维码
                Vendor('dangmianfu_demo_php.f2fpay.qrpay_test');
                $qrpay = new \Alipay;


                $result = $qrpay->qrpay2($outTradeNo, $subject, $totalAmount, $app_auth_token, $extend_params, $adata);
                $erwm = $result->qr_code;
                if ($erwm == '') {
                    // 该口碑码不可用
                    D('Kbsh')->where(['app_auth_token' => $app_auth_token])->save(['use_status' => 0, 'cause' => '该码不可用']);
                    echo json_encode(['info' => '请刷新重试！']);
                    die;
                }
                $data = [
                    'uid' => $uid,
                    'danhao' => $outTradeNo,
                    'pay_money' => $totalAmount,
                    // 'pay_time'=>time(),
                    'pay_type' => $pay_type,
                    'seller_id' => $one['user_id'],
                    'erwm' => $erwm,
                ];
                echo json_encode($data);
                die;

            }


        }
    }

    // 回款
    function save_data()
    {
        if (IS_POST) {
            $uid = I('post.uid');
            if (empty($uid) || $uid == 0) {
                echo json_encode(['info' => '该用户不存在！']);
                die;
            }
            unset($_POST['erwm']);
            $_POST['pay_time'] = time();
            $_POST['status'] = 0;
            $one = D('Dingdan')->where(['danhao' => I('post.danhao')])->find();
            if (empty($one)) {
                $id = D('Dingdan')->add(I('post.'));
            } else {
                $id = 1;
            }

            echo json_encode(['re' => $id]);
            die;

        }
    }

    // 不回款
    function save_data2()
    {
        if (IS_POST) {

            $uid = I('post.uid');


            if (empty($uid) || $uid == 0) {
                echo json_encode(['info' => '该用户不存在！']);
                die;
            }


            $pay_type = I('post.pay_type');
            $gather = D('Gather')->where("uid=" . $uid)->find();

            // echo json_encode(['info'=>$pay_type]);die;
            if (($pay_type == 2 && $gather['status_hb'] == 1) || ($pay_type == 3 && $gather['status_fq'] == 1)) {

            } else {
                echo json_encode(['info' => '该商户无此收款权限！']);
                die;
            }
            unset($_POST['erwm']);
            unset($_POST['pay_type']);
            $_POST['pay_time'] = time();
            $_POST['status'] = 0;
            $_POST['hk'] = 1;

            // echo json_encode($_POST);die;

            $id = D('Dingdan')->add(I('post.'));
            echo json_encode(['re' => $id]);
            die;

        }
    }


// 平台用户提现
    public function tixian_btn()
    {


        if (IS_POST) {
// print_r($_POST);die;
            $info = D('Tongdao')->find();
            if ($info['tixian'] == 0) {
                echo "系统已关闭提现。";
                die;
            }

            $uid = I('post.uid');
            $ga = D('Gather')->where("uid=$uid")->find();
            if ($ga['frozen'] == 0) {
                echo "该账号已被冻结，暂时无法提现。";
                die;
            }

            $alipay = I('post.alipay');
            $amount = I('post.money');
            $out_biz_no = date("YmdHis") . rand(100000, 999999);
            $one = D('Ptyh')->where('uid=' . $uid)->find();
            $payee_real_name = $one['name'];
            $payer_show_name = '创世';
            $time = time();

            // 判断单笔累计、用户余额是否超出、支付宝当日累计支付
            if (0.1 > $amount) {
                echo "提现金额不可低于0.1元。";
                die;
            }
            if ($amount > 50000) {
                echo "单笔提现不可超过5万。";
                die;
            }
            $gather = D('Gather')->where("uid=" . $uid)->find();
            if ($gather['money'] < ($amount)) {
                echo "提现金额不可超过用户余额。";
                die;

            }

            // 判断结束
            $time = time();
            $extract = array(
                'uid' => $uid,
                'money' => $amount,//存入的是不扣除手续费的金额
                'time' => $time,
                'status' => 0,
                'out_biz_no' => $out_biz_no,
            );
            $id = D('Extract')->add($extract);
            Vendor('dangmianfu_demo_php.f2fpay.qrpay_test');
            $transfer = new \Alipay;
            $remark = '商户提现';
            $_amount = $amount;//实际到款

            $adata = D('Adata')->where(['id' => 1])->find();

            // 查看当日提现总金额  超过95万则使用另一个支付宝
            $time1 = strtotime(date("Ymd", strtotime("+1 day")));
            $time2 = strtotime(date("Ymd"));
            $money = D('Dingdan')->where("status=1 and pay_time between $time2 and $time1")->sum('pay_money');

            $result = $transfer->transfer8($out_biz_no, $alipay, $_amount, $remark, $payee_real_name, $payer_show_name, $adata);

            if (empty($result->sub_msg) && $result->out_biz_no == $out_biz_no) {
                // 说明是成功的   1、提现状态修改 2、提现次数、用户余额修改
                $out_no = $result->out_biz_no;
                $order_id = $result->order_id;
                $extract = D('Extract')->where('out_biz_no=' . $out_no)->find();

                D('Extract')->where('out_biz_no=' . $out_no)->save(['status' => 1, 'order_id' => $order_id]);

                $one = D('Gather')->where('uid=' . $extract['uid'])->find();
                $money = $one['money'] - $amount;
                $count = $one['count'] + 1;

                D('Gather')->where('uid=' . $extract['uid'])->save(['money' => $money, 'count' => $count]);

                D('Extract')->where('id=' . $extract['id'])->save(['t_money' => $money]);

                $data = [
                    'name' => $payee_real_name,
                    'time' => date("Y-m-d H:i:s", $time),
                    'money' => $amount,
                    'remark' => '提现单号：' . $out_biz_no . '，如有疑问请在“创世”微信公众号点击“创世钱包->人工客服”进行咨询。',
                ];
                $member = D('Ptyh')->where(['uid' => $uid])->find();
                if (!empty($member)) {
                    D('Notice')->weixin(11, $member['wx_uid'], $data);
                } else {
                    // echo '未绑定微信';die;
                }

                echo 1;
                die;

            } else {
                D('Extract')->where('out_biz_no=' . $out_biz_no)->save(['cause' => $result->sub_msg]);
                echo "提现失败";
                die;

            }


        } else {


            $uid = $_GET['uid'];

            // if($uid!=8300){
            //     echo '提现功能暂时关闭';die;
            // }

            $time = intval(date('G'));

            // if($time>=9&&$time<=17){//凌晨到晚上8打开提现

            // }else{

            //         echo "<script>alert('12月30号至1月1号的提现时间为9:00-17:00，请悉知。');location.href='http://qnm.upslope.cn/index.php?m=Weixin&c=Koubei&a=index';</script>";die;

            // }


            $list = D('Ptyh')->where("uid=" . $uid)->find();
            $gather = D('Gather')->where("uid=" . $uid)->find();
            C('WEB_SITE_TITLE', '提现');
            $this->assign('list', $list);
            $this->assign('gather', $gather);
            $this->display();
        }

    }

    public function up()
    {
        $upload = new \Think\Upload;
        $result = $upload->upload($_FILES);
        $file = $result['file']['savepath'];
        $file .= $result['file']['savename'];

        if (I('get.type') == 2) {
            $id = D('Shensu')->add(['pic' => $file]);
        } else {
            $id = D('Tousu')->add(['pic' => $file]);
        }


        echo $file;
        die;
    }


    public function tousu()
    {
        // echo 123;die;
        if (IS_POST) {

            $trade_no = I('post.trade_no');
            $_POST['time'] = time();
            $complainant = I('post.complainant');
            $pic = I('post.pic');

            $ts = D('Tousu')->where("trade_no='{$trade_no}'")->find();
            if (!empty($ts)) {
                // 已存在同一笔订单的投诉，则不许再次发起
                echo "<script>confirm('该笔订单已被投诉，请勿重复提交！');location.href='http://qnm.upslope.cn/index.php?m=Weixin&c=User&a=tousu&complainant={$complainant}';</script>";
                die;
            }
            if ($trade_no == '' || I('post.account') == '' || I('post.complain') == '' || I('post.phone') == '' || I('post.cause') == '') {
                echo "<script>confirm('请填写完整信息！！');location.href='http://qnm.upslope.cn/index.php?m=Weixin&c=User&a=tousu&complainant={$complainant}';</script>";
                die;

            }

            if (empty($pic)) {//没有详情图不予处理
                echo "<script>confirm('请务必上传详情图，否则投诉不予处理！');location.href='http://qnm.upslope.cn/index.php?m=Weixin&c=User&a=tousu&complainant={$complainant}';</script>";
                die;
            }


            $one = D('Dingdan')->where("trade_no='{$trade_no}'")->find();
            if (empty($one)) {
                // 查不到交易数据则删除图片及数据
                unlink(I('post.pic'));
                D('Tousu')->where(['pic' => $pic])->delete();
                echo "<script>confirm('没有查到该笔订单，请仔细填写订单号！');location.href='http://qnm.upslope.cn/index.php?m=Weixin&c=User&a=tousu&complainant={$complainant}';</script>";
                die;
            } else {
                $id = D('Tousu')->where(['pic' => $_POST['pic']])->save(I('post.'));
                if ($id > 0) {
                    echo "<script>confirm('投诉提交成功，请耐心等候！');location.href='http://qnm.upslope.cn/index.php?m=Weixin&c=User&a=tousu&complainant={$complainant}';</script>";
                    die;
                } else {
                    // 保存不成功则删除图片及数据
                    unlink(I('post.pic'));
                    D('Tousu')->where(['pic' => $pic])->delete();
                    echo "<script>confirm('投诉提交失败，请重新提交！');location.href='http://qnm.upslope.cn/index.php?m=Weixin&c=User&a=tousu&complainant={$complainant}';</script>";
                    die;
                }
            }


        } else {

            $complainant = $_GET['complainant'];
            $this->assign('complainant', $complainant);
            C('WEB_SITE_TITLE', '投诉');
            $this->display();
        }

    }

    public function shensu()
    {


        if (IS_POST) {

            $shop_name = I('post.shop_name');
            $uid = I('post.uid');


            unset($_POST['shop_name']);
            unset($_POST['uid']);

            foreach ($_POST as $key => $v) {
                $pic = $v['pic'];
                $v['time'] = time();
                $v['shop_name'] = $shop_name;
                $v['uid'] = $uid;
                $id[] = D('Shensu')->where("pic='{$pic}'")->save($v);
            }


            if (in_array(0, $id)) {
                foreach ($_POST as $key => $v) {
                    unlink($v);
                    D('Shensu')->where("pic='{$v}'")->delete();

                }
                echo "<script>confirm('申诉提交失败，请重新提交！');location.href='http://qnm.upslope.cn/index.php?m=Weixin&c=User&a=shensu&id={$shop_name}';</script>";
                die;

            } else {
                echo "<script>confirm('申诉提交成功，请耐心等候！');location.href='http://qnm.upslope.cn/index.php?m=Weixin&c=User&a=shensu&id={$shop_name}';</script>";
                die;
            }

        } else {
            $shop_name = $_GET['id'];
            $one = D('Ptyh')->where("shop_name='{$shop_name}'")->find();
            $ts = D('Tousu')->where("complainant='{$shop_name}'")->select();
            foreach ($ts as $key => $v) {
                $no = $v['trade_no'];
                $ss = D('Shensu')->where("trade_no='$no'")->find();
                if (!empty($ss)) {//有这条申诉，表示这个申诉已经处理完成或处理中，则不需要再次展示
                    unset($ts[$key]);

                }

            }
            $this->assign('ts', $ts);
            $this->assign('one', $one);
            $this->assign('shop_name', $shop_name);
            C('WEB_SITE_TITLE', '申诉');
            $this->display();
        }

    }


    function test()
    {
        echo 1;
        die;
    }


}
